﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MTQ.Common
{
    public class JsonSerializerHelper
    {
        public static List<T> GetJsonList<T>(string jsonString)
        {
            if (jsonString == null)
            {
                return new List<T>();
            }
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(jsonString);
            object o = serializer.Deserialize(new JsonTextReader(sr), typeof(List<T>));
            List<T> list = o as List<T>;
            return list;
        }
        public static T GetJsonEntity<T>(string jsonString) where T : class
        {
            if (jsonString == null)
            {
                return default(T);
            }
            JsonSerializer serializer = new JsonSerializer();
            StringReader sr = new StringReader(jsonString);
            object o = serializer.Deserialize(new JsonTextReader(sr), typeof(T));
            T entity = o as T;
            return entity;
        }

        public static string GetString<T>(List<T> list) {
            string str = "";
            str=JsonConvert.SerializeObject(list);            
            return str; ;

        }

        public static string GetString<T>(T list)
        {
            string str = "";
            str = JsonConvert.SerializeObject(list);
            return str; ;

        }
    }
}
